﻿using MediatR;
using W.Purchase.Domain.DTO;
using W.Purchase.Read.API.AppLication.Command.ProcurementCommands;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler.ProcurementCommandsHandler
{
    public class ReturnFanCommandHandler : IRequestHandler<ReturnFanCommand, ApiResult<ProcurementFanDTO>>
    {
        private readonly ILogger<ReturnFanCommandHandler> _logger;
        private readonly IProcurementRepository _procurementRepository;
        private readonly IBuyProductsRepository _buyProductsRepository;
        private readonly IVendorRepository _vendorRepository;
        public ReturnFanCommandHandler(ILogger<ReturnFanCommandHandler> logger, IProcurementRepository procurementRepository, IBuyProductsRepository buyProductsRepository, IVendorRepository vendorRepository)
        {
            _logger = logger;
            _procurementRepository = procurementRepository;
            _buyProductsRepository = buyProductsRepository;
            _vendorRepository = vendorRepository;
        }
        public async Task<ApiResult<ProcurementFanDTO>> Handle(ReturnFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ProcurementFanDTO> result = new ApiResult<ProcurementFanDTO>();
            try
            {
                _logger.LogInformation("反填采购退货成功");
                var products = await _buyProductsRepository.GetAllAsaync();
                var procurement = from a in await _procurementRepository.GetAllAsaync()
                                  join b in await _vendorRepository.GetAllAsaync() on a.VendorNo equals b.VendorNo
                                  where a.IsDelete == false && a.ProcurementId == request.procurementId
                                  select new ProcurementFanDTO
                                  {
                                      ProcurementNo = a.ProcurementNo,
                                      ProcurementId = a.ProcurementId,
                                      ProcurementTypeName = a.ProcurementTypeName,
                                      AssociateNumber = a.AssociateNumber,
                                      VendorNo = a.VendorNo,
                                      VendorName = b.VendorName,
                                      ClientName = a.ClientName,
                                      ProcurementPerson = a.ProcurementPerson,
                                      ProcurementPhone = a.ProcurementPhone,
                                      Harvestay = a.Harvestay,
                                      ProcurementRemark = a.ProcurementRemark,
                                      SingleMaker = a.SingleMaker,
                                      SingleTime = a.SingleTime,
                                      CreateTime = a.CreateTime,
                                      BuyProductsData = products.Where(x => x.ProcurementId == a.ProcurementId && x.IsReturn == true).Select(x => new BuyProductsData
                                      {
                                          BuyProductsId = x.BuyProductsId,
                                          ProcurementId = x.ProcurementId,
                                          ProductNo = x.ProductNo,
                                          ProductName = x.ProductName,
                                          ProcurementState = x.ProcurementState,
                                          ProcurementStateText = x.ProcurementState.ToString(),
                                          IsRecorded = x.IsRecorded,
                                          IsReturn = x.IsReturn,
                                          ReturnNo = x.ReturnNo,
                                          ReturnsNum = x.ReturnsNum,
                                          Batch = x.Batch,
                                          ProductSpec = x.ProductSpec,
                                          UnitName = x.UnitName,
                                          Price = x.Price,
                                          BuyProductsNum = x.BuyProductsNum,
                                          BuyProductsTotal = x.BuyProductsTotal,
                                      }).ToList()
                                  };
                result.Data = procurement.FirstOrDefault();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
            }
            return result;
        }
    }
}
